package example.book.controller;

import example.book.model.BookInfo;
import example.book.model.UserInfo;
import example.book.server.UserServer;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserServer userServer;
    @RequestMapping("/login")
    public boolean login(String name, String password, HttpSession session) {
        //账号密码为空,返回false
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
            return false;
        }
        //账户不存在
        UserInfo userInfo = userServer.getUserByName(name);
        if(userInfo == null) {
            return false;
        }
        //账号密码不正确
        if(!password.equals(userInfo.getPassword())) {
            return false;
        }
        //存储在session中
        userInfo.setPassword("");
        session.setAttribute("session_key",userInfo);
        return true;
    }
}
